home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-26 | 6.2 KB | 266 lines | [TEXT/CWIE] |
- /*------------------------------------------------------------------------------
- #
- # NewsTicker, my Hack for 1997
- #
- # Ticker.cp - the application shell to do cool stuff in
- #
- # based on sample source code from long, long ago
- #
- ------------------------------------------------------------------------------*/
-
- #include <PLStringFuncs.h> /* some special string handling stuff */
- #include <strings.h>
- #include <Power.h>
- #include <OpenTransport.h>
- #include "TickerGlobals.h" /* bring in all the #defines for Ticker */
- #include "TickerWindowHandler.h"
- #include "TickerAppEvts.h"
-
- /* The "g" prefix is used to emphasize that a variable is global. */
- Boolean gHasPowerManager;
-
- /* Prototypes of the routines that actually do the work */
- void EventLoop( void );
- void DoEvent( EventRecord *event );
- void DoMenuCommand( long menuResult );
- void Initialize( void );
-
- #pragma segment Main
- main()
- {
- SetApplLimit(GetApplLimit()-32768); //give an additional 32K to stack
- MaxApplZone(); /* expand the heap so code segments load at the top */
-
- Initialize(); /* initialize the program */
-
- EventLoop(); /* call the main event loop */
- }
-
-
- /* Get events forever, and handle them by calling DoEvent.
- Get the events by calling WaitNextEvent. */
-
- #pragma segment Main
- void EventLoop()
- {
- Boolean gotEvent;
- EventRecord event;
- WindowPtr aWindow;
- RgnHandle mouseRgn = NewRgn();
-
- InitWHandler();
-
- SetRectRgn (mouseRgn, -32767, -32767, 32767, 32767);
-
- do {
-
- if (gHasPowerManager)
- {
- //IdleUpdate ();
- }
- PollWHandler(event.where);
-
- gotEvent = WaitNextEvent(everyEvent, &event, 1, nil);
-
- if ( gotEvent ) {
- DoEvent(&event);
- }
- } while ( gDoneFlag ); /* loop forever; we quit via ExitToShell */
-
- gotEvent = true;
- CloseWHandler();
- } /*EventLoop*/
-
- #pragma segment Main
- void DoEvent(EventRecord* event)
- {
-
- short part, err;
- WindowPtr window;
- Boolean hit;
- char key;
- Point aPoint;
-
- switch ( event->what ) {
- case 0:
- break;
- case kHighLevelEvent:
- err = AEProcessAppleEvent( event ) ;
- break;
- case mouseDown:
- part = FindWindow(event->where, &window);
- switch ( part ) {
- case inMenuBar: /* process a mouse menu command (if any) */
- DoMenuCommand(MenuSelect(event->where));
- break;
- case inSysWindow: /* let the system handle the mouseDown */
- SystemClick(event, window);
- break;
- }
- break;
- case keyDown:
- case autoKey: /* check for menukey equivalents */
- key = event->message & charCodeMask;
- if ( event->modifiers & cmdKey ) /* Command key down */
- if ( event->what == keyDown ) {
- DoMenuCommand(MenuKey(key));
- }
- break;
- case osEvt:
- switch ((event->message >> 24) & 0x0FF) { /* high byte of message */
- case kSuspendResumeMessage: /* suspend/resume is also an activate/deactivate */
- gInBackground = (event->message & kResumeMask) == 0;
- break;
- }
- break;
- }
- } /*DoEvent*/
-
- /* This is called when an item is chosen from the menu bar (after calling
- MenuSelect or MenuKey). It performs the right operation for each command.
- It is good to have both the result of MenuSelect and MenuKey go to
- one routine like this to keep everything organized. */
-
- #pragma segment Main
-
- static pascal Boolean AboutAlertFilter(DialogPtr pDialog,
- EventRecord *macEvent,
- short *itemHit)
- {
- GrafPtr theport;
-
- GetPort(&theport);
- JustHandleWindow(); //scroll the window below the about window
- SetPort(theport);
-
- if (macEvent->what==keyDown)
- {
- switch (macEvent->message & charCodeMask)
- {
- case 0x03: // enter key
- case 0x0D: // return key
- *itemHit = 1;
- return true;
- break;
- default:
- return false;
- }
- }
- return false;
- }
-
- static void DoTheAlert(void)
- {
- ModalFilterUPP OurFilterUPP;
- OurFilterUPP = NewModalFilterProc(AboutAlertFilter);
- Alert(128, OurFilterUPP);
- DisposeRoutineDescriptor(OurFilterUPP);
- }
-
- void DoMenuCommand(long menuResult)
- {
- short menuID; /* the resource ID of the selected menu */
- short menuItem; /* the item number of the selected menu */
- short itemHit;
- Str255 daName;
- short daRefNum;
- Boolean handledByDA;
-
- menuID = HiWord(menuResult); /* use macros for efficiency to... */
- menuItem = LoWord(menuResult); /* get menu item number and menu number */
- switch ( menuID ) {
- case mApple:
- switch ( menuItem ) {
- case iAbout: /* bring up alert for About */
- DoTheAlert();
- break;
- default: /* all non-About items in this menu are DAs */
- /* type Str255 is an array in MPW 3 */
- GetMenuItemText(GetMenuHandle(mApple), menuItem, daName);
- daRefNum = OpenDeskAcc(daName);
- break;
- }
- break;
- case mFile:
- switch ( menuItem ) {
- case iConfigure:
- DoConfigure ();
- break;
- case iQuit:
- gDoneFlag = false;
- break;
- }
- break;
- case mEdit: /* call SystemEdit for DA editing & MultiFinder */
- handledByDA = SystemEdit(menuItem-1); /* since we don’t do any Editing */
- break;
- }
- HiliteMenu(0); /* unhighlight what MenuSelect (or MenuKey) hilited */
- } /*DoMenuCommand*/
-
-
- #pragma segment Initialize
-
- static Boolean GotPowerManager( void )
- {
- long pmgrAttributes;
- Boolean routinesExist;
-
- routinesExist = false;
- if (Gestalt(gestaltPowerMgrAttr, &pmgrAttributes)==noErr)
- if (pmgrAttributes & (1<<gestaltPMgrDispatchExists))
- if (PMSelectorCount() >= 7) /* do the first 8 routines exist? */
- routinesExist = true;
- return routinesExist;
- }
-
-
- void Initialize()
- {
- Handle menuBar;
- WindowPtr window;
- long total, contig;
- EventRecord event;
- short count;
- long x;
- OSErr err;
- unsigned long now;
-
- gInBackground = false;
-
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- InstallAEHandlers();
- menuBar = GetNewMBar(rMenuBar); /* read menus into menu bar */
- if ( menuBar == nil ) ExitToShell();
- SetMenuBar(menuBar); /* install menus */
- DisposeHandle(menuBar);
- AppendResMenu(GetMenuHandle(mApple), 'DRVR'); /* add DA names to Apple menu */
- DrawMenuBar();
-
- InitOpenTransport();
-
- gHasPowerManager = GotPowerManager();
-
- for (count = 1; count <= 3; count++)
- {
- if (WaitNextEvent(everyEvent, &event, 5, nil))
- {
- if (event.what==kHighLevelEvent)
- {
- err = AEProcessAppleEvent( &event ) ;
- }
-
- }
- }
- } /*Initialize*/
-
- #pragma segment Main
-
-